<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>管理组列表</legend>
    </fieldset>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

<script type="text/html" id="action">
    <a href="{:url('groupAccess')}?id={{d.id}}" class="layui-btn layui-btn-xs layui-btn-normal">配置规则</a>
    <a href="javascript:;" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">{:lang('edit')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
</script>
<script type="text/html" id="topBtn">
    <a href="javascript:;" class="layui-btn layui-btn-sm" lay-event="add">{:lang('add')}管理组</a>
</script>
<script>
    layui.use('table', function () {
        var table = layui.table, $ = layui.jquery;
        var tableIns = table.render({
            elem: '#list',
            url: '{:url("adminGroup")}',
            method: 'post',
            toolbar: '#topBtn',
            title: '管理组列表',
            cols: [[
                {field: 'id', title: '{:lang("id")}', width: 80, fixed: true, sort: true},
                {field: 'title', title: '管理组名', width: 180},
                {field: 'create_time', title: '添加时间', width: 200, sort: true},
                {width: 260, align: 'center', toolbar: '#action'}
            ]]
        });
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('你确定要删除该分组吗？', function (index) {
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('groupDel')}", {id: data.id}, function (res) {
                        layer.close(loading);
                        layer.close(index);
                        if (res.code == 1) {
                            layer.msg(res.msg, {time: 1000, icon: 1});
                            obj.del();
                        } else {
                            layer.msg(res.msg, {time: 1000, icon: 2});
                        }
                    });
                });
            }
            if (obj.event === 'edit') {
                layer.prompt({
                    value: data.title,
                    title: '修改管理组',
                }, function (value, index, elem) {
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('groupEdit')}", {id: data.id, title: value}, function (res) {
                        layer.close(loading);
                        layer.close(index);
                        if (res.code == 1) {
                            layer.msg(res.msg, {time: 1000, icon: 1});
                            obj.update({
                                title: value
                            });
                        } else {
                            layer.msg(res.msg, {time: 1000, icon: 2});
                        }
                    });
                });
            }
        });


        table.on('toolbar(list)', function (obj) {
            if (obj.event === 'add') {
                layer.prompt({title: '请输管理组名称'}, function (value, index, elem) {
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('groupAdd')}", {title: value}, function (res) {
                        layer.close(loading);
                        layer.close(index);
                        if (res.code == 1) {
                            layer.msg(res.msg, {time: 1000, icon: 1});
                            tableIns.reload();
                        } else {
                            layer.msg(res.msg, {time: 1000, icon: 2});
                        }
                    });
                });
            }
        });
    });

</script>
</body>
</html>